﻿using _1._12._2015_College.Data;
using _1._12._2015_College.Data.Models;
using _1._12._2015_College.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity.Migrations;

namespace _1._12._2015_College.Controllers
{
    public class RatingController : ApiController
    {

        public IHttpActionResult Get()
        {
            List<RatingViewModel> models = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                models = db.Ratings.Select(r => new RatingViewModel
                {
                   CollegeId = r.CollegeId,
                   Text = r.Text,
                   NumRating = r.NumRating
                }).ToList();
            }
            return Ok(models);
        }

        public IHttpActionResult Get(int schoolId)
        {
            List<RatingViewModel> models = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                models = db.Ratings.Where(r => r.CollegeId == schoolId).Select(r => new RatingViewModel
                {
                    CollegeId = r.CollegeId,
                    Text = r.Text,
                    NumRating = r.NumRating
                }).ToList();
            }
            return Ok(models);
        }
       


        // add new review
        public IHttpActionResult Post(RatingViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Rating rating = new Rating();
                rating.CollegeId = model.CollegeId;
                rating.Text = model.Text;
                rating.NumRating = model.NumRating;
                db.Ratings.Add(rating);
                db.SaveChanges();

                // find college and updata it's statictics
                College college = db.Colleges.FirstOrDefault(c => c.CollegeId == model.CollegeId);
                college.NumberOfRatings ++;
                college.TotalRatings += model.NumRating;
                college.AvgRating = college.TotalRatings / college.NumberOfRatings;
                
                db.SaveChanges();
            }
            return Ok();
        }

        //public IHttpActionResult Put(CollegeViewModel model)
        //{
        //    using (ApplicationDbContext db = new ApplicationDbContext())
        //    {
        //        College college = db.Colleges.FirstOrDefault(c => c.CollegeId == model.CollegeId);
        //        college.Name = model.Name;
        //        college.State = model.State;
        //        college.City = model.City;
        //        college.State = model.State;
        //        college.AvgRating = model.AvgRating;

        //        db.SaveChanges();
        //    }
        //    return Ok();
        //}

        //public IHttpActionResult Delete(int id)
        //{
        //    using (ApplicationDbContext db = new ApplicationDbContext())
        //    {
        //        College college = db.Colleges.FirstOrDefault(c => c.CollegeId == id);
        //        db.Colleges.Remove(college);
        //        db.SaveChanges();
        //    }
        //    return Ok();
        //}


    }
}
